<?php

$cmd=$_REQUEST['cmd'];
switch($cmd)
{
    
    case 1:
        search_nurses();
        break;
    case 2:
        addNurse();
        break;
    case 3:
        getAllNurses();
        break;
    case 4:
        editNurse();
        break;
    case 5:
        deleteNurse();
        break;
}

function search_nurses(){
        if(!isset($_REQUEST['st'])){
		//return error
		echo '{"result":0,"message": "search did not work."}';
	} 
        if(isset($_REQUEST['st'])){
            include_once('nurses.php');
            $obj1 = new nurses();
            
            $search_text = $_REQUEST['st'];
           
            
            if($obj1->search_nurse_by_name($search_text)){
               $row=$obj1->fetch();
                echo '{"result":1,"nurses":[';	//start of json object
                while($row){
                        echo json_encode($row);	//convert the result array to json object
                        $row=$obj1->fetch();
                        if($row){
                                echo ",";					//if there are more rows, add comma 
                        }
                }
                echo "]}";
            }
            else{
		//return error
		echo '{"result":0,"message": "search did not work."}';
		return;
            } 
        }
    }
    
    
    function getAllNurses(){
        include_once('nurses.php');
            $obj1 = new nurses();
            $obj1->get_nurses();
            
           
                $row=$obj1->fetch();
                echo '{"result":1,"nurses":[';	//start of json object
                while($row){
                        echo json_encode($row);			//convert the result array to json object
                        $row=$obj1->fetch();
                        if($row){
                                echo ",";					//if there are more rows, add comma 
                        }
                }
            echo "]}";
    }
    
    function addNurse(){
        if(isset($_REQUEST['fn'])){
            include_once('nurses.php');
            include_once 'users.php';
            
            $fname = $_REQUEST['fn'];
            $sname = $_REQUEST['sn'];
            $gender = $_REQUEST['gender'];
            $department = $_REQUEST['dpt'];
            $username = $_REQUEST['user'];
            $password = $_REQUEST['pass'];
            

            $obj = new nurses();
            $user = new users();
            $admin = 0;
            if(!$obj->add_nurse($sname, $fname, $gender, $department)){
                echo '{"result":0,"message": "add did not work."}';
            }
            else{
                $id = $obj->get_insert_id();
                $user->add_users($username, $password, $admin, $id);
                echo '{"result":1,"message": "added sucessfully "}';
            }
                
        }
    }
    
    function editNurse(){
        if(isset($_REQUEST['fn'])){
            include_once("nurses.php");
            
            $fname = $_REQUEST['fn'];
            $sname = $_REQUEST['sn'];
            $gender = $_REQUEST['gender'];
            $department = $_REQUEST['dpt'];
            $nid = $_REQUEST['nid'];
            
           
            $obj = new nurses();
            if(!$obj->edit_nurse($nid, $sname, $fname, $gender, $department)){
               echo '{"result":0,"message": "update did not work."}';
            }
            else{
                 echo '{"result":1,"message": "updated sucessfully "}';
            }
                
        }
    }
    
    function deleteNurse(){
        include_once 'nurses.php';
        include_once 'users.php';
        if(isset( $_REQUEST["id"])){
            $nid = intval($_REQUEST['id']);
            
            $user = new users();
            $obj = new nurses();
            if(!$obj->delete_nurse($nid)){
               echo '{"result":0,"message": "delete did not work."}';

            }
            else {
                $user->delete_user($nid);
                echo '{"result":1,"message": "deleted succesfully."}';

            }
        }
    }
?>
